<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 14:07
 */

namespace app\api\controller;


use app\service\CartService;
use app\service\GoodsService;
use app\common\model\Cart as CartModel;
use app\common\model\Goods as GoodsModel;
use app\common\model\GoodsSku as GoodsSkuModel;

class Cart extends Base
{
    //查询购物车
    public function index(){
        $where['uid']=$this->uid;
        $input = input();
        $shop_type = $input['shop_type']?$input['shop_type']:1;
        $where['zkhb_cart.shop_type']=$shop_type;
        $list = CartService::S()->queryList($where);
        $new = array();
        foreach($list as $k=>$v) {
            if($v['sku_id']) {
                //如果sku无效，购物车直接不展示
                $sku = GoodsSkuModel::getOneById($v['sku_id']);
                if(!$sku || $sku['status']==0) {
                    unset($list[$k]);
                    CartModel::where('id',$v['id'])->delete();
                    continue;
                }
                $list[$k]['sku_name'] = $sku['name'];
                $list[$k]['price'] = $sku['price'];
                $new[] = $list[$k];
            }else{
                //如果购物车没有sku,但是商品现在确存在sku,说明是很久之前加入的，也不展示
                $sku = GoodsSkuModel::getOne([
                    'goods_id' => $v['goods_id'],
                    'status' => 1
                ]);
                if($sku) {
                    unset($list[$k]);
                    CartModel::where('id',$v['id'])->delete();
                    continue;
                }else{
                    $new[] = $list[$k];
                }
            }
        }
        $backData['cart']= $new;
        $backData['cart']=GoodsService::dealGoods($backData['cart']);
        $backData['goods']=GoodsService::S()->queryGoodsInfoByWhere(array('is_tuijian'=>1,'onsale'=>1,'shop_type'=>$shop_type),Goods::$goods_base_filed);//查询出推荐商品
        $backData['goods']=GoodsService::dealGoods($backData['goods']);
        $this->ajax(200,'查询成功',$backData);
    }

    //添加购物车
    public function add_cart(){
        $input = input();
        $cart['goods_id']=$input['goods_id'];
        $cart['sku_id']=$input['sku_id'];
        $goods = GoodsModel::getOneById($cart['goods_id']);
        if(!$cart['goods_num']) {
            $cart['goods_num'] = $goods['buy_step'];
        }
        $cart['goods_num'] = max($cart['goods_num'],$goods['buy_min']);
        $goods_num = $cart['goods_num'];
        if(empty($cart['goods_id']) && empty($cart['sku_id'])){
            $this->ajax(202,'商品ID不能为空');
        }
        if($cart['sku_id']) {
            $sku = GoodsSkuModel::getOneById($cart['sku_id']);
            if(!$sku || $sku['status']==0) {
                $this->ajax(202,'规格不存在');
            }
            //$goods=GoodsService::S()->findGoodsInfoByGoodsId($sku['goods_id'],'onsale,stock,shop_type');
            $goods = GoodsModel::getOneById($sku['goods_id'],'onsale,stock,shop_type');
            if(!$goods){
                $this->ajax(202,'商品不存在');
            }if($goods['onsale']==0){
                $this->ajax(202,'商品已下架');
            }if($sku['stock']<$goods_num){
                $this->ajax(202,'商品库存不足');
            }
            $cart['goods_id'] = $sku['goods_id'];
        }else{
            $sku = GoodsSkuModel::getOne([
                'goods_id' => $cart['goods_id'],
                'status' => 1
            ]);
            if($sku) {
                $this->ajax(202,'请提交sku_id');
            }
            unset($cart['sku_id']);
            $goods = GoodsModel::getOneById($cart['goods_id'],'onsale,stock,shop_type');
            //$goods=GoodsService::S()->findGoodsInfoByGoodsId($cart['goods_id'],'onsale,stock,shop_type');
            if(!$goods){
                $this->ajax(202,'商品不存在');
            }if($goods['onsale']==0){
                $this->ajax(202,'商品已下架');
            }if($goods['stock']<$goods_num){
                $this->ajax(202,'商品库存不足');
            }
        }
        //查看用户是否存在购物车
        $cart['uid']=$this->uid;
        $w = $cart;
        unset($w['goods_num']);
        $cartInfo=CartModel::getOne($w);
        $cart['create_time'] = time();
        if($cartInfo){
            //$re=CartService::S()->saveCart(array('goods_num'=>$goods_num),$cartInfo['id']); //自增商品数量
            if($input['type']=='dec') {
                $re = CartModel::where('id',$cartInfo['id'])->setDec('goods_num',$goods_num);
            }else{
                $re = CartModel::where('id',$cartInfo['id'])->setInc('goods_num',$goods_num);
            }
            if($re===false){
                $re=false;
            }else{
                $re=$cartInfo['id'];;
            }
        }else{
            $cart['shop_type'] = $goods['shop_type'];
            $cart['goods_num']=$goods_num;//input('post.goods_num');
            //$cart['create_time']=date("Y-m-d H:i:s");//date('Y-m-d H:i:s');
            $re=CartService::S()->addCart($cart);
        }
        if($re===false){
            $this->ajax(202,'添加购物车失败');
        }else{
            $this->ajax(200,'添加购物车成功',array(
                'id'=>$re,
                //'cart_count'=>CartService::S()->countCart($this->uid)
                'cart_count' => CartModel::getCount([
                    'uid' => $this->uid,
                    'shop_type' => $goods['shop_type']
                ])
            ));
        }
    }

    //删除购物车
    public function del_cart(){
        $cart_ids=input('post.cart_ids');
        if(empty($cart_ids)){
            $this->ajax(202,'删除购物车ID不能为空');
        }
        $re=CartService::S()->delCart($cart_ids,$this->uid);
        if($re){
            $this->ajax(200,'删除购物车成功');
        }else{
            $this->ajax(202,'删除购物车失败');
        }
    }

}